package com.lf.interceptor;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * @Title: 管理台系统如果运行在互联网服务器上面，则不能被请求
 * @Description:
 * @Auther: xhq
 * @Version: 1.0
 * @create 2019/3/26 10:35
 */
@Slf4j
@Component
public class LocalInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    public StringRedisTemplate stringRedisTemplate;
    /**
     * 是否阻止提交,fasle阻止,true放行
     * @return
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //用 getLocalHost() 方法创建的InetAddress的对象
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()){
                NetworkInterface netInterface = allNetInterfaces.nextElement();
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()){
                    InetAddress ip = addresses.nextElement();
                    if (ip != null
                            && ip instanceof Inet4Address
                            && !ip.isLoopbackAddress() //loopback地址即本机地址，IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
                            && ip.getHostAddress().indexOf(":")==-1){
//                        LOG.warn("本机的IP = " + ip.getHostAddress());
                        if ("172.16.16.3".equals(ip.getHostAddress()) || "106.55.147.43".equals(ip.getHostAddress())){
//                            LOG.warn("The system is under attack : "+ request.getRequestURL());
                            JSONObject result = new JSONObject();
                            result.put("code","500");
                            result.put("message","不要攻击我啦,再攻击我就发起反攻了");
                            response.setCharacterEncoding("UTF-8");
                            response.setContentType("application/json; charset=utf-8");
                            response.getWriter().write(result.toString());
                            response.getWriter().close();
                            //拦截之后跳转页面
                            //String formRequest = request.getRequestURI();
                            //request.setAttribute("myurl", formRequest);
                            //request.getRequestDispatcher("/WebRoot/common/error/jsp/error_message.jsp").forward(request, response);
                            return false;
                        }
                        return super.preHandle(request, response, handler);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

}